package top.wyix;

import java.util.Scanner;

/**
 * 计算器应用程序主类
 *
 * @author wangdx
 * @since 2025/9/8
 */
public class App {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        Scanner scanner = new Scanner(System.in);

        System.out.println("简易计算器");
        System.out.println("请输入表达式(例如：4+2)");
        System.out.println("支持的运算符：+、-、*、/");
        System.out.println("输入exit退出");

        while (true) {
            System.out.print("> ");
            String input = scanner.nextLine();

            if (input.equals("exit")) {
                System.out.println("谢谢使用，再见！");
                break;
            }

            try {
                String[] parts = input.split(" ");
                if (parts.length != 3) {
                    System.out.println("输入格式错误，请使用空格分割数字和运算符");
                    continue;
                }
                double num1 = Double.parseDouble(parts[0]);
                String operator = parts[1];
                double num2 = Double.parseDouble(parts[2]);

                double result = 0;
                switch (operator) {
                    case "+":
                        result = calculator.add(num1, num2);
                        break;
                    case "-":
                        result = calculator.subtract(num1, num2);
                        break;
                    case "*":
                        result = calculator.multiply(num1, num2);
                        break;
                    case "/":
                        result = calculator.divide(num1, num2);
                        break;
                    default:
                        System.out.println("不支持的运算符");
                        continue;
                }
                System.out.println("结果：" + result);
            } catch (Exception e) {
                System.out.println("输入错误，请重新输入");
            }
        }
    }
}
